package com.forum.demo.controller;

import com.forum.demo.common.AppResult;
import com.forum.demo.common.ResultCode;
import com.forum.demo.model.Board;
import com.forum.demo.services.IBoardService;
import com.forum.demo.utils.checkFailUtils;
import jakarta.annotation.Resource;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/board")
public class BoardController {

    @Value("${bit-forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Resource
    private IBoardService boardService;

    @RequestMapping("/topList")
    public AppResult<List<Board>> topList() {
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        if (boards == null) {
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }
    @RequestMapping("/getById")
    public AppResult<Board> getById(@NonNull Long boardId) {
        Board board = boardService.selectById(boardId);
        if (board == null || board.getDeleteState() == 1) {
            checkFailUtils.checkFail(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }
        return AppResult.success(board);
    }
}
